﻿using System;
using System.Data;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BO.News;
using ChannelVN.Cached;

namespace GUI.Controls.News
{
    public partial class LatestNews : System.Web.UI.UserControl
    {
        public int Top = 30;
        private const string LiFormat = "<li>{4}<h5><a href=\"{0}\" title=\"{2}\">{1} <span class=\"datetime\">({3})</span></a></h5></li>";
        private const int ImageWidth = 60;
        private const int ImageHeight = 60;
        public bool ExecutePageLoad = true;
        public string Title = "Tin mới cập nhật";

        protected void Page_Load(object sender, EventArgs e)
        {
            var dt = NewsBO.GetInstance().GetNewsByPosition(top: Top, typeId: DefinedConstants.News.TypeIDs.TinTrangChu);
            Visible = DataBind(dt);
        }

        private bool DataBind(DataTable dt)
        {

            if (dt != null && dt.Rows.Count > 0)
            {
                var sb = new System.Text.StringBuilder();
                foreach (DataRow r in dt.Rows)
                {
                    var avatar = PresentationUtils.Photo.BuildNewsAvatar(r, ImageWidth, ImageHeight);

                    sb.Append(string.Format(
                            LiFormat,
                            r[SqlConstants.NewsPublish.Url],
                            r[SqlConstants.NewsPublish.Title],
                            r[SqlConstants.NewsPublish.EncodedTitle],
                            PresentationUtils.Object2DateTime(r[SqlConstants.NewsPublish.DistributionDate].ToString()).ToString("HH:mm dd/MM/yyyy"),
                            avatar
                        ));
                }
                ltrData.Text = sb.ToString();
                ltrTitle.Text = Title;
                return true;
            }
            return false;

        }

    }
}
